<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>数据字典</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin/style/admin.css" media="all">
    <style>
        .demo-tree-box {
            padding: 10px;
            overflow: auto;
        }
        .tree-txt-active {
            background-color: lightblue;
        }

        #test2 {
            padding: 10px 5px;
            overflow: auto;
        }

        .layui-tree-entry .layui-tree-txt {
            padding: 0 5px;
            border: 1px transparent solid;
            text-decoration: none !important;
        }

        .layui-tree-entry.ew-tree-click .layui-tree-txt {
            background-color: #fff3e0;
            border: 1px #FFE6B0 solid;
        }
    </style>
</head>

<body style="overflow-y: hidden">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-sm2 layui-hide-xs" style="height: 100vh; padding-bottom: 25px; overflow: hidden;">
                <div class="layui-card" style="height: 100%">
                    <div class="layui-card-body">
                        <!-- 数据表格1 -->
                        <div id="test2" class="demo-tree demo-tree-box"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm10">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <!-- 表格工具栏2 -->
                        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding:0">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <input type="text" name="title" placeholder="请输入分类名称" autocomplete="new-password"
                                        class="layui-input">
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit
                                        lay-filter="form-search">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <!-- 数据表格2 -->
                        <table id="dataItem-manage" lay-filter="dataItem-manage"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'table', 'tree', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table,
                tree = layui.tree,
                util = layui.util,
                admin = layui.admin;
            window.areaName = "dataItem-manage";
            var tree_data = [];
            admin.req({
                type: 'get',
                async: false,
                url: '/api/DataItem/GetAllDataItems',
                success: function (r) {
                    if (r.code == 200) {
                        tree_data = r.data;
                    }
                }
            });
            //仅节点左侧图标控制收缩
            tree.render({
                elem: '#test2',
                data: tree_data,
                onlyIconControl: true,
                click: function (obj) {
                    if (obj.data.parentId == '0') {
                        table.reload(areaName, {
                            where: {
                                queryJson: JSON.stringify({ title: obj.data.title })
                            }
                        })
                    }
                }
            });
            $("#test2 .layui-tree-txt").click(function (e) { // 在页面任意位置点击而触发此事件
                if ($(e.target).attr('class') === "layui-tree-txt") { // 防止因为点击展开按钮把已选中的样式取消
                    $(".layui-tree-txt").removeClass("tree-txt-active"); // 移除点击样式
                    $(e.target).addClass("tree-txt-active"); // e.target表示被点击的目标
                }
            });
            //监听搜索
            form.on('submit(form-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload(areaName, {
                    where: { queryJson: JSON.stringify(field) },
                    page: {
                        curr: 1
                    }
                });
            });
            //终端列表
            table.render({
                elem: '#' + areaName,
                toolbar: $.getToolButtons(),
                defaultToolbar: [],
                url: '/api/DataItem/GetDataItemPageList',
                cols: [
                    [
                        { field: 'number', title: '序号', width: 100, align: 'center', type: 'numbers' },
                        { field: 'cItemTitle', title: '分类标题', width: 150 },
                        { field: 'cItemCode', title: '分类编码', minWidth: 150 },
                        { field: 'cItemName', title: '字典名称', width: 150 },
                        { field: 'cItemValue', title: '字典代码', minWidth: 150 },
                        { field: 'cSortCode', title: '排序', width: 100 },
                        { field: 'cEnabled', title: '是否启用', width: 100, templet: function (r) { return r.cEnabled ? "是" : "否" } },
                        { title: '操作', width: 200, align: 'center', fixed: 'right', templet: () => $.getRowButtons() }
                    ]
                ],
                page: true,
                limit: 15,
                height: 'full-110',
                text: '对不起，加载出现异常！'
            });
            var selectRow = null;
            table.on('row(' + areaName + ')', function (obj) {
                selectRow = obj.data;
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            });
            var editForm = function (id) {
                layer.open({
                    type: 2,
                    title: '字典管理',
                    content: 'dataItemform.html?id=' + id,
                    area: ['700px', '470px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        var that = this.yes.arguments[1];
                        that.find('a.layui-layer-btn0').css("pointer-events", "none");
                        setTimeout(() => {
                            that.find('a.layui-layer-btn0').css("pointer-events", "")
                        }, 2000)
                        layero.find('iframe').contents().find('#form-submit').trigger('click');
                    }
                });
            }
            var editTypeForm = function (id) {
                $.OpenTab({
                    type: 2,
                    title: '字典分类管理',
                    content: 'dataItemTypeform.html?id=' + id,
                    area: ['700px', '470px'],
                    btn: ['确定', '取消']
                });
            }
            var del = function (type) {

                let ids = [];
                if (type == 1 || type == undefined) {
                    if (selectRow == null) return layer.msg('请选择数据');
                    ids = [selectRow.cId];
                } else if (type == 2) {
                    var checkData = table.checkStatus(areaName).data; //得到选中的数据
                    if (checkData.length === 0) return layer.msg('请选择数据');
                    ids = checkData.map(t => t.cId);
                } else {
                    return layer.msg('请选择删除类型');
                }

                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        type: 'post',
                        url: '/api/DataItem/DelDataItem',
                        data: { ids: ids.join(','), },
                        success: function (data) {
                            if (data.code == 200) {

                                location.reload();
                                layer.close(index);
                                layer.msg('已删除!', { icon: 1, time: 1000 });
                            } else {
                                layer.close(index);
                                layer.msg(data.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            }
            //事件
            util.event('lay-active', {
                'x-add': () => editForm(),
                'x-delete': () => del(),
                'x-batch-delete': () => del(2),
                'x-edit':  function () {
                    if (selectRow.cParentId == '0') editTypeForm(selectRow.cId);
                    else editForm(selectRow.cId);
                },
                'x-addType': () => editTypeForm()
            });
        });
    </script>
</body>

</html>